#include "stdafx.h"

#include "../../UnitTest/TestMgr.h"
#include "../../UnitTest/Test.h"
#include "../CfgMgr.h"
#include "../Config.h"
#include "../CfgUtil.h"

using namespace pb::cfg;

void testBuffer()
{
    Buffer<char> buf1(5, 100);
    Buffer<char> buf2(buf1);
    Buffer<char> buf3(0);
    buf3 = buf2;

    assert(buf1.data() != buf2.data());
    assert(buf1.data() != buf3.data());
    assert(buf1 == buf2);
    assert(buf2 == buf3);
    
    buf1[3] = 99;
    buf2[3] = 101;
    buf3[3] = 101;

    assert(buf1 != buf2);
    assert(buf1 != buf3);
    assert(buf2 == buf3);
}

void testCfg()
{
    std::wstring fileName(L"unitTest_cfg.pty");
    const std::wstring path = Base::UnitTest::TestMgr::get()->exePath();

    Config* cfg = pb::cfg::CfgMgr::get()->newCfg(path + fileName);

    assert(cfg->setValue("Japan", "Toyata"));
    assert(cfg->setValue("China", "Cherry"));
    assert(cfg->setValue("German", "BMW"));

    assert(cfg->setValue("Line1", 101));
    assert(cfg->setValue("Line2", -202));
    assert(cfg->setValue("Line3", -303));

    assert(!cfg->setValue("Line1", "Gelly"));
    assert(!cfg->setValue("China", 55));

    pb::cfg::CfgMgr::get()->saveCfg(cfg);
    pb::cfg::CfgMgr::get()->unloadCfg(cfg);
    assert(!cfg);

    Config* cfg2 = pb::cfg::CfgMgr::get()->loadCfg(path + fileName);
    assert(cfg2 != cfg);

    std::string str;
    __int32 int32 = 0;

    assert(cfg2->getValue("Japan", str)  && str == "Toyata");
    assert(cfg2->getValue("China", str)  && str == "Cherry");
    assert(cfg2->getValue("German", str) && str == "BMW");

    assert(cfg2->getValue("Line1", int32) && int32 == 101);
    assert(cfg2->getValue("Line2", int32) && int32 == -202);
    assert(cfg2->getValue("Line3", int32) && int32 == -303);

    assert(!cfg2->getValue("Line1", str));
    assert(!cfg2->getValue("China", int32));
}

static Base::UnitTest::Test kTestBuffer(testBuffer);
static Base::UnitTest::Test kTestConfig(testCfg);